package com.apes.fn.scm.settle.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.income.model.IncomeItem;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.IncomeType;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.settle.model
 * @ClassName: PlatformSettleRebate
 * @Author: pyb
 * @CreateTime: 2020-02-27 18:34
 * @Description: 供应商对账单返利明细
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PlatformSettleRebate.CONST_TABLE_NAME)
public class PlatformSettleRebate extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_platform_settle_Rebate";
    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "供应商对账单返利明细编码", padding = 8)
    @Comment("编码")
    private String id;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "platform_settle_id", referencedColumnName = "id")
    @Comment("结算单号")
    private PlatformSettle platformSettle;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_item_id", nullable = false, referencedColumnName = "id")
    @Comment("收入单明细号")
    private IncomeItem incomeItem;

    @Transient
    private String incomeId;

    public String getIncomeId() {
        if (incomeItem == null) return null;
        return incomeItem.getIncome().getId();
    }

    @Transient
    private PartyRole company;

    public PartyRole getCompany() {
        if (incomeItem == null) return null;
        return incomeItem.getIncome().getCompany();
    }

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_type_id", referencedColumnName = "id")
    @Comment("收入类型编码")
    private IncomeType incomeType;

    @Column(name = "income_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("收入单据金额")
    private double incomeAmount = 0;

    @Column(name = "settle_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("已结算金额")
    private double settleAmount = 0;

    @Column(name = "actual_amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("扣款金额")
    private double actualAmount = 0;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "count_date")
    @Comment("计算日期")
    private Date countDate;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "reconcile_begin_date")
    @Comment("开始对账日期")
    private Date reconcileBeginDate;

    @JSONField(format = DateUtil.SHOW_DATE_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "reconcile_end_date")
    @Comment("结束对账日期")
    private Date reconcileEndDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "in_come_category", referencedColumnName = "id")
    @Comment("收入类别")
    private Enumeration inComeCategory;

}
